package org.example.sbdemo.controller;

import org.example.sbdemo.DTO.UserDTO;
import org.example.sbdemo.response.Response;
import org.example.sbdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/show")
    public Response<List<UserDTO>> findAll() {
        return Response.newSuccess(userService.findAll());
    }

    @GetMapping("/user/{id}")
    public Response<UserDTO> findById(@PathVariable Integer id) {
        return Response.newSuccess(userService.findById(id));
    }

    @PostMapping("/add")
    public Response<Integer> add(@RequestBody UserDTO userDTO) {
        return Response.newSuccess(userService.addUser(userDTO));
    }

    @DeleteMapping("/delete/{id}")
    public Response<Void> deleteById(@PathVariable Integer id) {
        userService.deleteById(id);
        return Response.newSuccess(null);
    }

    @PutMapping("/update/{id}")
    public Response<UserDTO> update(@PathVariable Integer id, @RequestBody UserDTO userDTO) {
        // 确保路径参数id与请求体中的id一致
        userDTO.setId(id);
        return Response.newSuccess(userService.update(userDTO));
    }

    @GetMapping("/male")
    public Response<List<UserDTO>> findByGenderM() {
        return Response.newSuccess(userService.findAllByGenderM());
    }

    @GetMapping("/female")
    public Response<List<UserDTO>> findByGenderF() {
        return Response.newSuccess(userService.findAllByGenderF());
    }
}
